﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.MovieRecorder.CUiMRCRecorderSettingsWindow
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui.WPF;
using UiInternalControls.WPF;


namespace DnProcessSimulateCommands.MovieRecorder;

public class CUiMRCRecorderSettingsWindow : TxWindow, IComponentConnector, IStyleConnector
{
  private CApMRCRecorderSettingsViewModel m_viewModel;
  private CUiMRCRecorderSettingsPreviewOverlay m_previewWindow;
  internal System.Windows.Controls.TextBox m_path;
  internal System.Windows.Controls.Button btnBrowse;
  internal System.Windows.Controls.RadioButton m_defaultResolutionBtn;
  internal TxSlider m_resolutionSlider;
  internal TxNumericEditBoxControl m_resolutionWidth;
  internal TxNumericEditBoxControl m_resolutionHeight;
  internal System.Windows.Controls.RadioButton m_screenRadioBtn;
  internal Expander operationExpander;
  internal Expander skipWeldingExpander;
  internal TxSlider m_compressionlider;
  internal Expander overlayExpander;
  internal Grid LayoutRoot;
  internal Grid Overlay;
  internal TxNumericEditBoxControl m_resizeImage;
  internal System.Windows.Controls.TextBox txtOverlay;
  internal Popup popupOverlayText;
  internal System.Windows.Controls.ListView lstItems;
  internal System.Windows.Controls.ComboBox overlayComboBox;
  internal Expander visibilityOptionsExpander;
  internal System.Windows.Controls.Button btnCreate;
  internal System.Windows.Controls.Button btnClose;
  private bool _contentLoaded;

  public CUiMRCRecorderSettingsWindow(CApMRCRecorderSettings recorderSettings)
    : this(recorderSettings, SettingsView.MovieRecorder)
  {
  }

  public CUiMRCRecorderSettingsWindow(
    CApMRCRecorderSettings recorderSettings,
    SettingsView settingsViewModel)
  {
    this.InitializeComponent();
    switch (settingsViewModel)
    {
      case SettingsView.MovieManager:
        this.m_viewModel = (CApMRCRecorderSettingsViewModel) new CApMRCRecorderMovieManagerSettingsViewModel(recorderSettings);
        break;
      case SettingsView.FileAttach:
        this.m_viewModel = (CApMRCRecorderSettingsViewModel) new CApMRCRecorderFileAttachSettingsViewModel(recorderSettings);
        break;
      default:
        this.m_viewModel = new CApMRCRecorderSettingsViewModel(recorderSettings);
        break;
    }
    if (this.m_viewModel.OverlayEnabled)
      this.m_previewWindow = new CUiMRCRecorderSettingsPreviewOverlay(this.m_viewModel.Preview);
    ((FrameworkElement) this).DataContext = (object) this.GetTemporaryViewModel(recorderSettings);
  }

  private CApMRCRecorderSettingsViewModel GetTemporaryViewModel(
    CApMRCRecorderSettings recorderSettings)
  {
    return new CApMRCRecorderSettingsViewModel(new CApMRCRecorderSettings()
    {
      ExpanderSettings = {
        CaptureAreaExpanded = recorderSettings.ExpanderSettings.CaptureAreaEnabled,
        OverlayExpanded = recorderSettings.ExpanderSettings.OverlayEnabled,
        TimingExpanded = recorderSettings.ExpanderSettings.TimingEnabled,
        CompressionExpanded = recorderSettings.ExpanderSettings.CompressionEnabled,
        VisibilityOptionsExpanded = recorderSettings.ExpanderSettings.VisibilityOptionsEnabled
      }
    });
  }

  private void btnClose_Click(object sender, RoutedEventArgs e)
  {
    ((Window) this).DialogResult = new bool?(false);
    ((Window) this).Close();
  }

  private void TxWindow_Loaded(object sender, RoutedEventArgs e)
  {
    this.m_path.GetBindingExpression(System.Windows.Controls.TextBox.TextProperty).UpdateSource();
    if (this.m_previewWindow == null)
      return;
    this.m_previewWindow.Owner = (Window) this;
    this.m_previewWindow.Show();
  }

  private void TxWindow_ContentRendered(object sender, EventArgs e)
  {
    ((FrameworkElement) this).MinWidth = ((FrameworkElement) this).MaxWidth = ((FrameworkElement) this).ActualWidth;
    ((FrameworkElement) this).DataContext = (object) this.m_viewModel;
  }

  private void TxWindow_Closed(object sender, EventArgs e)
  {
    if (this.m_previewWindow == null)
      return;
    this.m_previewWindow.Close();
  }

  private void btnCreate_Click(object sender, RoutedEventArgs e)
  {
    string errors;
    if (this.IsValid(out errors))
    {
      ((Window) this).DialogResult = new bool?(true);
      ((Window) this).Close();
    }
    else
    {
      if (string.IsNullOrEmpty(errors))
        return;
      int num = (int) TxMessageBox.ShowModal(errors, MRCStringTable.MOVIE_RECORDER_SETTINGS_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
  }

  private void btnBrowse_Click(object sender, RoutedEventArgs e)
  {
    using (TxFolderBrowserDialog folderBrowserDialog = new TxFolderBrowserDialog(MRCStringTable.MOVIE_RECORDER_SETTINGS_BROWSE_DESTINATION))
    {
      if (!string.IsNullOrEmpty(this.m_viewModel.Destination))
        folderBrowserDialog.InitialFolder = this.m_viewModel.Destination;
      if (folderBrowserDialog.ShowDialog((Window) this) != 1)
        return;
      this.m_viewModel.Destination = CApMRCRecorderUtilities.NormalizeFilePath(folderBrowserDialog.SelectedFolder);
    }
  }

  private bool IsValid(out string errors)
  {
    bool flag = true;
    StringBuilder stringBuilder = new StringBuilder();
    if (!this.IsValid((DependencyObject) this.m_path))
    {
      flag = false;
      stringBuilder.AppendLine(MRCStringTable.MOVIE_RECORDER_SETTINGS_INVALID_DESTINATION);
    }
    errors = stringBuilder.ToString();
    return flag;
  }

  private bool IsValid(DependencyObject obj)
  {
    return !Validation.GetHasError(obj) && LogicalTreeHelper.GetChildren(obj).OfType<DependencyObject>().All<DependencyObject>(new Func<DependencyObject, bool>(this.IsValid));
  }

  private void m_resolutionWidth_CanChangeValue(
    object sender,
    TxNumericEditBoxCtrl_CanChangeValueEventArgs args)
  {
    args.CanChange = this.m_viewModel.ValidResoulutionWidth((uint) args.NewValue);
  }

  private void m_resolutionHeight_CanChangeValue(
    object sender,
    TxNumericEditBoxCtrl_CanChangeValueEventArgs args)
  {
    args.CanChange = this.m_viewModel.ValidResoulutionHeight((uint) args.NewValue);
  }

  private void EditLogo_MouseDown(object sender, MouseButtonEventArgs e)
  {
    this.ChooseOverlayImage(this.m_viewModel.OverlayImagePath);
  }

  private void RemoveLogo_MouseDown(object sender, MouseButtonEventArgs e)
  {
    this.m_viewModel.OverlayImagePath = string.Empty;
  }

  private void AddLogo_MouseDown(object sender, MouseButtonEventArgs e)
  {
    if (this.m_viewModel.AllowEditOverlayImage)
      return;
    this.ChooseOverlayImage(string.Empty);
  }

  private void ChooseOverlayImage(string imagePath)
  {
    using (OpenFileDialog openFileDialog = new OpenFileDialog())
    {
      string directoryName = Environment.SpecialFolder.Desktop.ToString();
      string str = string.Empty;
      object obj = (object) null;
      if (imagePath == string.Empty)
      {
        TxWindowSettings.RestoreWindowValue((TxWindow) this, "LastImagePath", ref obj);
        imagePath = obj != null ? obj.ToString() : string.Empty;
      }
      if (imagePath != string.Empty)
      {
        FileInfo fileInfo = new FileInfo(imagePath);
        if (fileInfo.Directory != null && fileInfo.Directory.Exists)
        {
          str = fileInfo.Name;
          directoryName = fileInfo.DirectoryName;
        }
      }
      openFileDialog.Filter = "PNG image files |*.png|BMP image files |*.bmp|JPEG image files |*.jpg";
      openFileDialog.Title = MRCStringTable.MOVIE_RECORDER_SETTINGS_OVERLAY_SELECT_FILE;
      openFileDialog.FileName = str;
      openFileDialog.InitialDirectory = directoryName;
      if (openFileDialog.ShowDialog() != DialogResult.OK)
        return;
      this.m_viewModel.OverlayImagePath = openFileDialog.FileName;
      TxWindowSettings.StoreWindowValue((TxWindow) this, "LastImagePath", (object) openFileDialog.FileName);
    }
  }

  private void ShowVariablesPopup(System.Windows.Rect placementRect)
  {
    this.popupOverlayText.PlacementRectangle = placementRect;
    this.popupOverlayText.IsOpen = true;
  }

  private void AddVariableToOverlayText(KeyValuePair<string, string> item)
  {
    this.popupOverlayText.IsOpen = false;
    int caretIndex = this.txtOverlay.CaretIndex;
    this.txtOverlay.Text = this.txtOverlay.Text.Insert(caretIndex, item.Key.Substring(1, item.Key.Length - 1));
    this.txtOverlay.CaretIndex = caretIndex + item.Key.Length - 1;
    this.txtOverlay.ScrollToHorizontalOffset(this.txtOverlay.GetRectFromCharacterIndex(this.txtOverlay.CaretIndex).Right);
  }

  private void popupOverlayText_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  {
    switch (e.Key)
    {
      case Key.Return:
        this.popupOverlayText.IsOpen = false;
        if (!(sender is System.Windows.Controls.ListBox listBox))
          break;
        this.AddVariableToOverlayText((KeyValuePair<string, string>) listBox.SelectedItem);
        break;
      case Key.Escape:
        this.popupOverlayText.IsOpen = false;
        break;
    }
  }

  private void popupOverlayText_Opened(object sender, EventArgs e)
  {
    this.lstItems.SelectedIndex = 0;
    this.lstItems.Focus();
    if (!(this.lstItems.ItemContainerGenerator.ContainerFromIndex(0) is System.Windows.Controls.ListViewItem listViewItem))
      return;
    listViewItem.Focus();
  }

  private void popupOverlayText_Closed(object sender, EventArgs e)
  {
    if (!(sender is Popup popup) || popup.PlacementTarget == null)
      return;
    popup.PlacementTarget.Focus();
  }

  private void lstItemsItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  {
    if (!(sender is System.Windows.Controls.ListViewItem listViewItem))
      return;
    this.AddVariableToOverlayText((KeyValuePair<string, string>) listViewItem.Content);
  }

  private void txtOverlay_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  {
    if (e.Key != Key.Escape || !this.popupOverlayText.IsOpen)
      return;
    this.popupOverlayText.IsOpen = false;
    e.Handled = true;
  }

  private void txtOverlay_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
  {
    if (!(sender is System.Windows.Controls.TextBox textBox) || textBox.CaretIndex == 0 || e.Key != Key.D5 || textBox.Text.Substring(textBox.CaretIndex - 1, 1).Trim().ToLower() != "%")
      return;
    this.ShowVariablesPopup(textBox.GetRectFromCharacterIndex(textBox.CaretIndex, true));
  }

  private void m_resizeImage_CanChangeValue(
    object sender,
    TxNumericEditBoxCtrl_CanChangeValueEventArgs args)
  {
    args.CanChange = this.m_viewModel.ValidSizeImage((uint) args.NewValue);
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    System.Windows.Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/movierecorder/settings/cuimrcrecordersettingswindow.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  internal Delegate _CreateDelegate(Type delegateType, string handler)
  {
    return Delegate.CreateDelegate(delegateType, (object) this, handler);
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        ((FrameworkElement) target).Loaded += new RoutedEventHandler(this.TxWindow_Loaded);
        ((Window) target).Closed += new EventHandler(this.TxWindow_Closed);
        ((Window) target).ContentRendered += new EventHandler(this.TxWindow_ContentRendered);
        break;
      case 2:
        this.m_path = (System.Windows.Controls.TextBox) target;
        break;
      case 3:
        this.btnBrowse = (System.Windows.Controls.Button) target;
        this.btnBrowse.Click += new RoutedEventHandler(this.btnBrowse_Click);
        break;
      case 4:
        this.m_defaultResolutionBtn = (System.Windows.Controls.RadioButton) target;
        break;
      case 5:
        this.m_resolutionSlider = (TxSlider) target;
        break;
      case 6:
        this.m_resolutionWidth = (TxNumericEditBoxControl) target;
        this.m_resolutionWidth.CanChangeValue += new TxNumericEditBoxCtrl_CanChangeValueEventHandler(this.m_resolutionWidth_CanChangeValue);
        break;
      case 7:
        this.m_resolutionHeight = (TxNumericEditBoxControl) target;
        this.m_resolutionHeight.CanChangeValue += new TxNumericEditBoxCtrl_CanChangeValueEventHandler(this.m_resolutionHeight_CanChangeValue);
        break;
      case 8:
        this.m_screenRadioBtn = (System.Windows.Controls.RadioButton) target;
        break;
      case 9:
        this.operationExpander = (Expander) target;
        break;
      case 10:
        this.skipWeldingExpander = (Expander) target;
        break;
      case 11:
        this.m_compressionlider = (TxSlider) target;
        break;
      case 12:
        this.overlayExpander = (Expander) target;
        break;
      case 13:
        this.LayoutRoot = (Grid) target;
        break;
      case 14:
        this.Overlay = (Grid) target;
        break;
      case 15:
        ((UIElement) target).MouseDown += new MouseButtonEventHandler(this.EditLogo_MouseDown);
        break;
      case 16 /*0x10*/:
        ((UIElement) target).MouseDown += new MouseButtonEventHandler(this.RemoveLogo_MouseDown);
        break;
      case 17:
        ((UIElement) target).MouseDown += new MouseButtonEventHandler(this.AddLogo_MouseDown);
        break;
      case 18:
        this.m_resizeImage = (TxNumericEditBoxControl) target;
        this.m_resizeImage.CanChangeValue += new TxNumericEditBoxCtrl_CanChangeValueEventHandler(this.m_resizeImage_CanChangeValue);
        break;
      case 19:
        this.txtOverlay = (System.Windows.Controls.TextBox) target;
        this.txtOverlay.KeyUp += new System.Windows.Input.KeyEventHandler(this.txtOverlay_KeyUp);
        this.txtOverlay.PreviewKeyDown += new System.Windows.Input.KeyEventHandler(this.txtOverlay_PreviewKeyDown);
        break;
      case 20:
        this.popupOverlayText = (Popup) target;
        this.popupOverlayText.Closed += new EventHandler(this.popupOverlayText_Closed);
        this.popupOverlayText.Opened += new EventHandler(this.popupOverlayText_Opened);
        break;
      case 21:
        this.lstItems = (System.Windows.Controls.ListView) target;
        this.lstItems.KeyDown += new System.Windows.Input.KeyEventHandler(this.popupOverlayText_KeyDown);
        break;
      case 23:
        this.overlayComboBox = (System.Windows.Controls.ComboBox) target;
        break;
      case 24:
        this.visibilityOptionsExpander = (Expander) target;
        break;
      case 25:
        this.btnCreate = (System.Windows.Controls.Button) target;
        this.btnCreate.Click += new RoutedEventHandler(this.btnCreate_Click);
        break;
      case 26:
        this.btnClose = (System.Windows.Controls.Button) target;
        this.btnClose.Click += new RoutedEventHandler(this.btnClose_Click);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IStyleConnector.Connect(int connectionId, object target)
  {
    if (connectionId != 22)
      return;
    ((Style) target).Setters.Add((SetterBase) new EventSetter()
    {
      Event = System.Windows.Controls.Control.MouseDoubleClickEvent,
      Handler = (Delegate) new MouseButtonEventHandler(this.lstItemsItem_MouseDoubleClick)
    });
  }
}
